<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ JSON.stringify(text) == '{}' ? '添加部门/学院' : '修改部门/学院' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form-model ref="ruleForm" :model="form" :rules="rules">
					<a-form-model-item label="名称" prop="Name"><a-input v-model="form.Name" /></a-form-model-item>
					<!-- <a-form-model-item label="编码" prop="Code"><a-input :disabled="JSON.stringify(text) == '{}' ? false : true" v-model="form.Code" /></a-form-model-item> -->
					<a-form-model-item label="上级部门" prop="ParentCode">
						<a-select v-model="form.ParentCode" style="width:100%" @change="departChange">
						  <a-select-option
						    v-for="(item, index) in ParentList"
						    :key="index"
							:disabled="item.Code==NowCode"
						    :value="item.Code"
						    >{{ item.Name }}</a-select-option
						  >
						</a-select>
					</a-form-model-item>
					<a-form-model-item label="类型" prop="Type">
						<a-select v-model="form.Type" style="width:100%" >
						  <a-select-option
						    v-for="(item, index) in TypeList"
						    :key="index"
						    :value="item.Code"
						    >{{ item.Name }}</a-select-option
						  >
						</a-select>
					</a-form-model-item>
					<a-form-model-item label="" class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ JSON.stringify(text) == '{}' ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-model-item>
				</a-form-model>
			</div>
			
		</div>
	</div>
</template>

<script>
import { AddDepart,UpdateDepart } from '@/api/follow';
export default {
	props: {
		text: Object,
		ParentList: Array
	},
	watch: {
		text: function(text) {
			if (JSON.stringify(text) == '{}') {
			} else {
				this.NowCode=text.Code
				this.form = text;
			}
		}
	},
	data() {
		return {
			NowCode:'',
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			DepartmenDropdowntList: [],
			form: {
				Name: '',
				Code: '',
				ParentCode: '',
				Type:""
			},
			TypeList:[{
				Name:'学院',
				Code:1
			},
			{
				Name:'部门',
				Code:2
			}
			],
			rules: {
				Name: [
					{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}
				],
				Type: [
					{
						required: true,
						message: '请选择类型',
						trigger: 'change'
					}
				],
				// ParentCode: [
				// 	{
				// 		required: true,
				// 		message: '请选择上级部门',
				// 		trigger: 'change'
				// 	}
				// ]
			}
		};
	},
	methods: {
		departChange(e){
			if(e===this.text.Code){
				this.form.ParentCode = ''
				this.$message.error('上级部门不能与当前部门一致！');
			}
		},
		closeFunction(data) {
			this.$emit('closeFun', data);
			this.form = {};
		},
		handleSubmit(e) {
			this.$refs.ruleForm.validate(async valid => {
				if (valid) {
					let data = this.form;
					let res = {}
					if (JSON.stringify(this.text) == '{}') {
						res = await AddDepart(data);
					} else {
						res = await UpdateDepart(data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					}else{
						this.$message.error(res.data.msg);
					}
				} else {
					return false;
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		margin: 20px 50px;
		display: flex;
	}
	/deep/.ant-form-item-label {
		width: 100px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}
	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}
/deep/.ant-form {
	margin-top: 50px;
	padding-bottom: 50px;
}
</style>
